package cn.net.yato.picture.biz.impl;

import cn.net.yato.picture.biz.SysBiz;
import cn.net.yato.picture.entity.LoginLog;
import cn.net.yato.picture.entity.User;
import cn.net.yato.picture.mapper.LoginLogMapper;
import cn.net.yato.picture.mapper.UserMapper;
import cn.net.yato.picture.util.TransactionalUtil;
import cn.net.yato.picture.util.log.MyPointcut;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @ClassName SysBizImpl
 * @Description 系统逻辑类
 * @Author yato
 * @Date 2020/2/19 10:14
 * @Version 1.0
 */
@Service
public class SysBizImpl implements SysBiz {

    @Resource
    private UserMapper userMapper;
    @Resource
    private LoginLogMapper loginLogMapper;

    @MyPointcut
    @Override
    public User loginBiz(String userName) {
        return userMapper.loginByUserName(userName);
    }

    @MyPointcut
    @Override
    public User loginByPhoneBiz(String phone) {
        return userMapper.loginByPhone(phone);
    }

    @MyPointcut
    @Override
    public User loginByEmailBiz(String email) {
        return userMapper.loginByEmail(email);
    }

    @MyPointcut
    @Override
    public User loginByWecatIdBiz(String wecatId) {
        return userMapper.loginByWecatId(wecatId);
    }

    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    @MyPointcut
    @Override
    public int addLoginLogBiz(LoginLog loginLog) {
        int temp = loginLogMapper.insert(loginLog);
        if (temp == 0){
            TransactionalUtil.rollback();
        }
        return temp;
    }
}
